package com.blb.controller;

import com.blb.pojo.User;
import com.blb.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("doLogin")
    public String doLogin(String username,
                          String password, HttpSession session, Model model){
        System.out.println("账号：" + username);
        System.out.println("密码：" + password);
        try {
            User user = userService.login(username,password);
            session.setAttribute("user",user);
            return "redirect:findAllEmployees";
        }catch (Exception ex){
            model.addAttribute("message","账号或密码错误");
            return "login";
        }
    }

//    @RequestMapping("doLogin")
//    public String doLogin(String username,
//                          String password, Model model){
//        System.out.println("账号：" + username);
//        System.out.println("密码：" + password);
//        if(!"admin".equals(username) ||
//            !"123".equals(password)){
//            model.addAttribute("message","账号或密码错误");
//            return "login";
//        }
//        return "employee";
//    }

//    @RequestMapping("doLogin")
//    public String doLogin(@RequestParam("username") String uname,
//                          @RequestParam("password") String pwd, Model model){
//        System.out.println("账号：" + uname);
//        System.out.println("密码：" + pwd);
//        if(!"admin".equals(uname) ||
//            !"123".equals(pwd)){
//            model.addAttribute("message","账号或密码错误");
//            return "login";
//        }
//        return "employee";
//    }

//    @RequestMapping("doLogin")
//    public String doLogin(User user, Model model){
//        System.out.println("账号：" + user);
//        if(!"admin".equals(user.getUsername()) ||
//            !"123".equals(user.getPassword())){
//            model.addAttribute("message","账号或密码错误");
//            return "login";
//        }
//        return "employee";
//    }

//    @RequestMapping("doLogin")
//    public String doLogin(User user, Map model){
//        System.out.println("账号：" + user);
//        if(!"admin".equals(user.getUsername()) ||
//                !"123".equals(user.getPassword())){
//            model.put("message","账号或密码错误");
//            return "login";
//        }
//        return "employee";
//    }

//    @RequestMapping("doLogin")
//    public String doLogin(User user, HttpSession model){
//        System.out.println("账号：" + user);
//        if(!"admin".equals(user.getUsername()) ||
//                !"123".equals(user.getPassword())){
//            model.setAttribute("message","账号或密码错误");
//            return "login";
//        }
//        return "employee";
//    }
}
